package com.woniuxy.controller.UserRelated;


import com.woniuxy.demo.Result;
import com.woniuxy.dto.*;
import com.woniuxy.dto.UserRelatedDto.UsersSelRolesDto;
import com.woniuxy.dto.commonDto.BaseDto;
import com.woniuxy.entity.RoleTable;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.PawnRoleException;
import com.woniuxy.param.*;
import com.woniuxy.param.UserRelatedParam.SaveRoleParam;
import com.woniuxy.param.UserRelatedParam.UpdateRoleParam;
import com.woniuxy.service.RoleTableService;
import com.woniuxy.util.ValidatedUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/role-table")
@Api(tags = "角色接口")
public class RoleTableController {
    @Resource
    private RoleTableService rts;


    /**
     * 修改角色数据
     *
     * @param urp
     * @param result
     * @return
     */
    @ApiOperation("修改角色数据")
    @PostMapping("/updateRole.do")
    public Result updateUser(@RequestBody @Validated UpdateRoleParam urp, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);

        boolean b = rts.updateRole(urp);
        if (b) {
            return Result.newInstance().setCode(200).setMsg("修改成功");
        }
        throw new PawnRoleException(CodeEnum.EMPOWER_FAIL.getMessage(), CodeEnum.EMPOWER_FAIL.getCode());
    }

    /**
     * 根据id查询用户
     *
     * @param roleId
     * @return
     * @throws Exception
     */
    @ApiOperation("根据id查询角色")
    @GetMapping("/findRoleById/{roleId}")
    @ApiImplicitParam(name = "roleId", value = "角色id")
    public Result findRoleById(@PathVariable Integer roleId) throws Exception {

        RoleDto dto = rts.findRoleById(roleId);

        return Result.newInstance().setMsg("查询成功").setCode(200).setData(dto);
    }

    /**
     * 添加新角色
     *
     * @param srp
     * @param result
     * @return
     */
    @ApiOperation("添加新角色")
    @PostMapping("/SaveRole.do")
    public Result SaveRole(@RequestBody @Validated SaveRoleParam srp, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);


        if (rts.SaveRole(srp)) {
            return Result.newInstance().setCode(200).setMsg("添加成功");
        }
        throw new PawnRoleException(CodeEnum.ROLE_SAVE_FAIL.getMessage(), CodeEnum.ROLE_SAVE_FAIL.getCode());

    }


    /**
     * 分页和模糊查询所有角色
     *
     * @param rp
     * @return
     */
    @ApiOperation("分页和模糊查询所有角色")
    @PostMapping("listRoles.do")
    public Result listRoles(@RequestBody RolesPageAndByKeyParam rp) {
        BaseDto<RoleTable> pageRoleDto = rts.listRoles(rp);

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(pageRoleDto);
    }

    /**
     * 查询当前用户绑定的所有角色和勾选状态
     *
     * @param userId
     * @return
     */
    @ApiOperation("查询当前用户绑定的所有角色和勾选状态")
    @ApiImplicitParam(name = "userId", value = "用户id")
    @PostMapping("/listRolesByUserId.do/{userId}")
    public Result listRolesByUserId(@PathVariable Integer userId) {
        UsersSelRolesDto dto = rts.listRolesByUserId(userId);
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(dto);
    }


    /**
     * 批量删除角色(逻辑删除)
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除角色(逻辑删除)")
    @PostMapping("/removeRoleByIds.do")
    public Result removeRoleByIds(@RequestBody List<Integer> ids) {
        if (rts.removeUserByIds(ids)) {
            return Result.newInstance().setCode(200).setMsg("删除成功");
        }
        throw new PawnRoleException(CodeEnum.ROLE_DELETE_FAIL.getMessage(), CodeEnum.ROLE_DELETE_FAIL.getCode());
    }
}

